package com.zyl.edthresholdtraining.service;

import com.zyl.edthresholdtraining.enums.DownLoadTypeEnum;
import com.zyl.edthresholdtraining.mapper.TimePartitionAllowMapper;
import com.zyl.edthresholdtraining.mapper.UserInfoMapper;
import com.zyl.edthresholdtraining.mapper.UserTestResultMapper;
import com.zyl.edthresholdtraining.util.ExcelUtils;
import com.zyl.edthresholdtraining.vo.UserTestExcelVO;
import com.zyl.edthresholdtraining.vo.UserTrainExcelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangtingfeng
 * @description
 * @date 2020/8/1/001
 */
@Slf4j
@Service
public class ExcelService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 导出数据库中的数据
     * 1.被试训练时的正确率与反应时报表导出
     *      用户编号  用户名称  开始测试时间  测试时间长度（ms） 正确率  平均反应时
     * 2.被试测试时的正确率与反应时报表导出
     *      用户编号  用户名称  开始测试时间  测试时间长度（ms） 正确率  平均反应时  时区
     * 3.被试每个组合图片的反应时和是否正确报表导出（训练）
     *      用户编号  用户名称
     * 4.被试每个组合图片的反应时和是否正确报表导出（正式测试）
     *
     * */
    public boolean downloadResource(String _type, HttpServletResponse response) throws Exception {
        int type = Integer.parseInt(_type);
        if (type == DownLoadTypeEnum.DOWNLOAD_ONE.getValue().intValue()) {
            List<UserTrainExcelVO> userTrainExcelVO = userInfoMapper.getUserTrainExcelVO();
            downloadUserTrainExcelVO(userTrainExcelVO,response);
        } else if (type == DownLoadTypeEnum.DOWNLOAD_TWO.getValue()) {
            List<UserTestExcelVO> userTestExcelVO = userInfoMapper.getUserTestExcelVO();
            downloadUserTestExcelVO(userTestExcelVO,response);
        } else if (type == DownLoadTypeEnum.DOWNLOAD_THREE.getValue()) {

        } else if (type == DownLoadTypeEnum.DOWNLOAD_FOUR.getValue()) {

        } else {

        }
        return false;
    }

    private void downloadUserTrainExcelVO(List<UserTrainExcelVO> list, HttpServletResponse response) throws Exception {
        Map<String, String> topMap = new LinkedHashMap<>();
        topMap.put("userId","被试编号");
        topMap.put("userName","被试名称");
        topMap.put("userSex","被试性别");
        topMap.put("createTime","每次训练开始时间");
        topMap.put("testTime","被试该次训练时间(ms)");
        topMap.put("rightRate","正确率(%)");
        topMap.put("avgTime","平均时长(ms)");
        String excelName = "被试训练时的正确率与反应时报表";
        ExcelUtils.listToExcel(list, topMap, excelName, response);
    }

    private void downloadUserTestExcelVO(List<UserTestExcelVO> list, HttpServletResponse response) throws Exception {
        Map<String, String> topMap = new LinkedHashMap<>();
        topMap.put("userId","被试编号");
        topMap.put("userName","被试名称");
        topMap.put("userSex","被试性别");
        topMap.put("createTime","每次测试开始时间");
        topMap.put("timePartition","被试进行测试时区");
        topMap.put("testTime","被试该次测试时间(ms)");
        topMap.put("rightRate","正确率(%)");
        topMap.put("avgTime","平均时长(ms)");
        String excelName = "被试测试时的正确率与反应时报表";
        ExcelUtils.listToExcel(list, topMap, excelName, response);
    }




}
